home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / gpp-1_42.lha / g++-1.42.0 / init.c < prev    next >
C/C++ Source or Header  |  1991-10-19  |  3KB  |  114 lines

  1. /* Startup code needed by GCC C++ output code.  */
  2. /* When compiling this file stand alone, use -fPIC */
  3. /* Copyright (C) 1991 Free Software Foundation, Inc.
  4.  
  5. This file is part of GNU CC.
  6.  
  7. GNU CC is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11.  
  12. GNU CC is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GNU CC; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. /* As a special exception, if you link this file with files
  22.    compiled with GCC to produce an executable, this does not cause
  23.    the resulting executable to be covered by the GNU General Public License.
  24.    This exception does not however invalidate any other reasons why
  25.    the executable file might be covered by the GNU General Public License.  */
  26.  
  27. /*  init.c: Originally developed by James Kempf for SUN,
  28.  *               adapted and commented for FSF by Heinz Seidl (hgs@cygnus.com)
  29.  */
  30.  
  31. /*************************************************************************
  32.  *              exports: void INITIALIZE_MODULE()
  33.  *                       void FINALIZE_MODULE()
  34.  *              imports: ep_fp * __function_list_addr
  35.  *
  36.  * Shared libraries should link a copy of this module into them. The
  37.  * dynamic initialization/finalization function can look up these
  38.  * functions and call them to do static ctor/dtor work.
  39.  * This file is also used for the main module.
  40.  ************************************************************************/
  41.  
  42. /* 
  43.  * The __C/DTOR_LIST__ have the folowing form:
  44.  * 
  45.  * entry_pt * __C/DTOR_LIST__[] = {
  46.  *    _first_entry_point,
  47.  *    _second_entry_point,
  48.  *    ...
  49.  *    0
  50.  * }
  51.  *
  52.  * Both lists are in _proper_ order; i.e. the destructor list is in reverse
  53.  * order of the constructor list.
  54.  *
  55.  * In case OLD_FORMAT is defined, init.c understands the format generated by
  56.  * gld 1.x and used by gnulib3.
  57.  */
  58.  
  59. #include "init_main.h"
  60.  
  61. extern ep_fp * __function_list_addr;
  62.  
  63. /* 
  64.  * Semaphore for indicating whether the library has already been initialized. 
  65.  */
  66. static int initialized = 0;
  67.  
  68. enum Direction { FORWARD, BACKWARD };
  69.  
  70. static inline
  71. void do_fns( ep_fp * fns, enum Direction d )
  72. {
  73.   ep_fp fn;
  74.   while( *fns )
  75.     {
  76.         if (d == FORWARD) 
  77.           fn = *fns++;
  78.         else
  79.           fn = *fns--;
  80.  
  81.         fn();
  82.     }
  83. }
  84.  
  85. void INITIALIZE_MODULE()
  86. {
  87.     if ( __function_list_addr && *__function_list_addr && ! initialized ) {
  88.     initialized = 1;
  89. #ifdef OLD_FORMAT
  90.     do_fns( __function_list_addr + 1, FORWARD);
  91. #else
  92.     do_fns( __function_list_addr, FORWARD );
  93. #endif
  94.     }
  95. }
  96.  
  97. void FINALIZE_MODULE()
  98. {
  99. #ifdef OLD_FORMAT
  100.      long offset = 0;
  101. #endif
  102.     if ( __function_list_addr && *__function_list_addr && initialized ) {
  103.      initialized = 0;
  104. #ifdef OLD_FORMAT
  105.      offset = (long) *__function_list_addr;
  106.      *__function_list_addr = 0;
  107.      __function_list_addr += offset;
  108.      do_fns( __function_list_addr, BACKWARD );
  109. #else
  110.      do_fns( __function_list_addr, FORWARD );
  111. #endif
  112.     }
  113. }
  114.